package com.leantech.android.browser.ui.shortcut.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.cscj.android.repository.db.entity.BrowseHistoryEntity
import com.cscj.android.repository.db.entity.FavoritesEntity
import com.cscj.android.utils.BitmapUtils.asBitmap
import com.leantech.android.browser.R
import com.leantech.android.browser.databinding.ItemCommonShortCutBinding
import com.leantech.android.browser.utils.onThrottleClick
import splitties.dimensions.dip
import splitties.resources.colorSL

class CommonShortCutAdapter(private val adapterCallback: CommonShortCutAdapterCallback) :
    ListAdapter<CommentShortCut, CommonShortCutAdapter.CommonShortCutViewHolder>(
        diffCallback
    ) {

    companion object {
        private const val PayloadAdded = 1
        private val diffCallback = object : DiffUtil.ItemCallback<CommentShortCut>() {
            override fun areItemsTheSame(
                oldItem: CommentShortCut, newItem: CommentShortCut
            ): Boolean {
                return (oldItem is CommentShortCut.FavoriteShortCut && newItem is CommentShortCut.FavoriteShortCut && oldItem.item.id == newItem.item.id) || (oldItem is CommentShortCut.HistoryShortCut && newItem is CommentShortCut.HistoryShortCut && oldItem.item.id == newItem.item.id)
            }

            override fun areContentsTheSame(
                oldItem: CommentShortCut, newItem: CommentShortCut
            ): Boolean {
                return (oldItem is CommentShortCut.FavoriteShortCut && newItem is CommentShortCut.FavoriteShortCut && oldItem.item == newItem.item && oldItem.added == newItem.added) || (oldItem is CommentShortCut.HistoryShortCut && newItem is CommentShortCut.HistoryShortCut && oldItem.item == newItem.item && oldItem.added == newItem.added)
            }

            override fun getChangePayload(
                oldItem: CommentShortCut, newItem: CommentShortCut
            ): Any? {
                if ((oldItem is CommentShortCut.FavoriteShortCut && newItem is CommentShortCut.FavoriteShortCut && oldItem.added != newItem.added) || (oldItem is CommentShortCut.HistoryShortCut && newItem is CommentShortCut.HistoryShortCut && oldItem.added != newItem.added)) {
                    return PayloadAdded
                }
                return super.getChangePayload(oldItem, newItem)
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommonShortCutViewHolder {
        val binding =
            ItemCommonShortCutBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return CommonShortCutViewHolder(binding)
    }

    override fun onBindViewHolder(holder: CommonShortCutViewHolder, position: Int) {
        holder.bind(getItem(position), adapterCallback)
    }

    override fun onBindViewHolder(
        holder: CommonShortCutViewHolder, position: Int, payloads: MutableList<Any>
    ) {
        if (payloads.isEmpty()) {
            super.onBindViewHolder(holder, position, payloads)
        } else {
            payloads.forEach { payload ->
                when (payload) {
                    PayloadAdded -> {
                        holder.onAddedPayload(getItem(position))
                    }

                    else -> {
                        super.onBindViewHolder(holder, position, payloads)
                    }
                }
            }
        }
    }

    interface CommonShortCutAdapterCallback {

        fun addCommentShortCut(data: CommentShortCut)

    }

    class CommonShortCutViewHolder(private val binding: ItemCommonShortCutBinding) :
        ViewHolder(binding.root) {

        fun bind(data: CommentShortCut, adapterCallback: CommonShortCutAdapterCallback) {
            when (data) {
                is CommentShortCut.HistoryShortCut -> {
                    bindHistory(data.item)
                }

                is CommentShortCut.FavoriteShortCut -> {
                    bindFavorites(data.item)
                }
            }
            onAddedPayload(data)
            binding.btnAdd.onThrottleClick {
                adapterCallback.addCommentShortCut(data)
            }
        }

        private fun bindHistory(data: BrowseHistoryEntity) {
            if (data.isRootItem()) {
                binding.icon.setImageResource(R.drawable.icon_history_root)
            } else {
                binding.icon.setImageBitmap(data.icon?.asBitmap())
            }
            binding.title.text = data.title
            binding.content.text = data.url
        }

        private fun bindFavorites(data: FavoritesEntity) {
            if (data.type == FavoritesEntity.TYPE_ROOT) {
                binding.icon.setImageResource(R.drawable.icon_favorites_root)
            } else {
                binding.icon.setImageBitmap(data.icon?.asBitmap())
            }
            binding.title.text = data.title
            binding.content.text = data.url
        }

        fun onAddedPayload(data: CommentShortCut) {
            val added = when (data) {
                is CommentShortCut.HistoryShortCut -> data.added
                is CommentShortCut.FavoriteShortCut -> data.added
                else -> false
            }

            binding.btnAdd.isEnabled = !added
            binding.btnAdd.setText(if (added) R.string.text_added else R.string.text_add)
            val borderColor = itemView.context.colorSL(R.color.state_blue_border_color)
            binding.btnAdd.setStrokeData(
                if (added) 0 else itemView.context.dip(1), borderColor
            )
        }
    }
}